//
//  AlumContentTableViewCell.m
//  u76ho
//
//  Created by m-air-01 on 16/1/29.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "AlumContentTableViewCell.h"

@implementation AlumContentTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(void)setModel:(PictureContentModel *)model
{
    _model = model;
    for (id i in self.contentView.subviews) {
        [i removeFromSuperview];
    }
    self.selected = NO;
    //日期lab
    UILabel * dateLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15), SIZETO6W(10), SCREEN_WIDTH-SIZETO6W(15)*2, 20)];
    //时间
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString * dataCh = model.addtime;
    NSString * dataStr = [dataCh substringToIndex:dataCh.length];
    NSDate * data = [[NSDate dateWithTimeIntervalSince1970:[dataStr integerValue]] dateByAddingTimeInterval:8*3600];
    NSString * lastStr = [NSString stringWithFormat:@"%@",data];
    NSArray * lastArr = [lastStr componentsSeparatedByString:@" "];
    dateLab.text = lastArr[0];
    [self.contentView addSubview:dateLab];
    
    //图片
    int j = 0;
    CGFloat picWidth = (SCREEN_WIDTH - SIZETO6W(15)*2 - SIZETO6W(10)*2)/3;
    if ([model.pic isKindOfClass:[NSString class]] && ![model.pic isEqualToString:@""]) {
        NSRange range = [model.pic rangeOfString:@"|"];
        if (range.location != NSNotFound) {
            NSArray * urlArr = [model.pic componentsSeparatedByString:@"|"];
            for (int i = 0; i<urlArr.count; i++) {
                int row=i/3;//行号
                if (i%3 == 0) {
                    j++;
                }
                int loc=i%3;//列号
                UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15)+(picWidth + SIZETO6W(10))*loc, dateLab.bottom + SIZETO6H(10)+(picWidth + SIZETO6H(10))*row, picWidth, picWidth)];
//                [imgV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,urlArr[i]]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
                
                [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,urlArr[i]]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    
                    
                    
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        float tempW = (image.size.height - image.size.width) >= 0 ? image.size.width : image.size.height;
                        imgV.image = [image croppedImageAtFrame:CGRectMake((image.size.height - image.size.width) >= 0 ? 0 : (image.size.width - image.size.height)/2, (image.size.width - image.size.height) >= 0 ? 0 : (image.size.height - image.size.width)/2 , tempW, tempW)];
                    });
                    
                    
                }];
                
                imgV.userInteractionEnabled = YES;
                //点击手势
                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
                tap.numberOfTapsRequired = 1;
                tap.numberOfTouchesRequired = 1;
                imgV.tag = i+1;
                [imgV addGestureRecognizer:tap];
                
                [self.contentView addSubview:imgV];
            }
        }
        else
        {
            UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15), dateLab.bottom+SIZETO6H(10), picWidth, picWidth)];
//            [imgV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
            
            [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    float tempW = (image.size.height - image.size.width) >= 0 ? image.size.width : image.size.height;
                    imgV.image = [image croppedImageAtFrame:CGRectMake((image.size.height - image.size.width) >= 0 ? 0 : (image.size.width - image.size.height)/2, (image.size.width - image.size.height) >= 0 ? 0 : (image.size.height - image.size.width)/2 , tempW, tempW)];
                });
                
            }];
            
            //点击手势
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            tap.numberOfTapsRequired = 1;
            tap.numberOfTouchesRequired = 1;
            imgV.tag = 1;
            imgV.userInteractionEnabled = YES;
            [imgV addGestureRecognizer:tap];

            [self.contentView addSubview:imgV];
            j = 1;
        }
    }
    else
    {
        picWidth = 0;
    }
    
    CGFloat height = [model.content heightOfSize:17 width:SCREEN_WIDTH-SIZETO6W(15)*2];
    
    //内容
    UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15), j*(picWidth + SIZETO6H(10))+dateLab.bottom, SCREEN_WIDTH-SIZETO6W(15)*2, height)];
    contentLab.text = model.content;
    contentLab.numberOfLines = 0;
    contentLab.font = [UIFont systemFontOfSize:15];
    contentLab.textColor = UIColorFromRGB(0x333944);
    [self.contentView addSubview:contentLab];
    
    
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"tapAction is did");
    int tag = (int)tap.view.tag;
    _cellBlock(_model,tag);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
